package cn.edu.jxau.test;

import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * 解法1
 * 
 * @author Fudashi
 */
//public class Main {
//
//	public static void main(String[] args) {
//
//		System.out.println(foo(12));
//	}
//
//	public static int foo(int n) {
//
//		if (n < 1) {
//			return 0;
//		}
//		int result = 0;
//		for (int i = 1; i <= n; i++) {
//			result += numberOf1(i);
//		}
//		return result;
//	}
//
//	private static int numberOf1(int n) {
//
//		int counter = 0;
//		while (n != 0) {
//			if (n % 10 == 1) {
//				counter++;
//			}
//			n = n / 10;
//		}
//		System.out.println(n + "	" + counter);
//		return counter;
//	}
//}

/**
 * 解法2 http://blog.csdn.net/yi_afly/article/details/52012593
 * 
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) {

		System.out.println(foo(534));
	}

	public static int foo(int n) {

		if (n < 0) {
			throw new IllegalArgumentException("n必须为非负数");
		}
		if (n < 1) {
			return 0;
		}
		int count = 0;
		int round = n;
		int weight = 0;
		int former = 0;
		int base = 1;
		while (round > 0) {

			weight = round % 10;
			round = round / 10;
			count = count + round * base;
			if(weight==1) {
				former = n%base;
				count = count + n%base +1;
			}else if(weight>1) {
				count = count + base;
			}
			base = base * 10;
		}
		return count;
	}
}
